package com.ll.app.controller;

import com.google.zxing.client.j2se.MatrixToImageWriter;
import com.google.zxing.common.BitMatrix;
import com.ll.app.api.req.AddRankingReqDTO;
import com.ll.app.api.req.ListRankingReqDTO;
import com.ll.app.api.req.PageRankingReqDTO;
import com.ll.app.api.req.RankingAuthenticationReqDTO;
import com.ll.base.dto.ApiResultDTO;
import com.ll.base.enums.QRCodeTypeEnum;
import com.ll.base.util.QRCodeUtils;
import com.ll.domain.ranking.biz.RankingDomainService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.OutputStream;

@Slf4j
@RestController
@RequestMapping("/ranking")
@RequiredArgsConstructor
@Api(tags = "排行榜API")
public class RankingController {

    private final RankingDomainService rankingDomainService;

    @ResponseBody
    @ApiOperation(value = "分页查询已认真的记录")
    @RequestMapping(value = "/page", method = RequestMethod.POST)
    public ApiResultDTO page(@RequestBody @Validated PageRankingReqDTO dto) {
        return ApiResultDTO.success(rankingDomainService.page(dto));
    }



    @ResponseBody
    @ApiOperation(value = "打榜")
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public ApiResultDTO add(@RequestBody @Validated AddRankingReqDTO addDTO) {
        rankingDomainService.add(addDTO);
        return ApiResultDTO.success();
    }

    @ResponseBody
    @ApiOperation(value = "查询所有的打榜记录")
    @RequestMapping(value = "/list", method = RequestMethod.POST)
    public ApiResultDTO list(@RequestBody @Validated ListRankingReqDTO dto) {
        return ApiResultDTO.success(rankingDomainService.list(dto));
    }

    @ResponseBody
    @ApiOperation(value = "认证二维码")
    @RequestMapping(value = "/getCode", method = RequestMethod.GET)
    public void getCode(@RequestParam(value = "id")  @Validated Long id,
                                HttpServletResponse response) throws IOException {
        // 设置响应流信息
        response.setContentType("image/jpg");
        response.setHeader("Pragma", "no-cache");
        response.setHeader("Cache-Control", "no-cache");
        response.setDateHeader("Expires", 0);

        OutputStream stream = response.getOutputStream();

        //type是1，生成活动详情、报名的二维码，type是2，生成活动签到的二维码
        String content = QRCodeTypeEnum.排行榜认证码.ordinal() + "|" +id;
        //获取一个二维码图片
        BitMatrix bitMatrix = QRCodeUtils.createCode(content);
        //以流的形式输出到前端
        MatrixToImageWriter.writeToStream(bitMatrix , "jpg" , stream);
    }

    @ResponseBody
    @ApiOperation(value = "删除")
    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    public ApiResultDTO add(@RequestParam(value = "id") Long id) {
        rankingDomainService.delete(id);
        return ApiResultDTO.success();
    }

    @ResponseBody
    @ApiOperation(value = "处理打榜认证")
    @RequestMapping(value = "/authentication", method = RequestMethod.POST)
    public ApiResultDTO authentication(@RequestBody RankingAuthenticationReqDTO reqDTO) {
        rankingDomainService.authentication(reqDTO);
        return ApiResultDTO.success();
    }

    @ResponseBody
    @ApiOperation(value = "赛季结束")
    @RequestMapping(value = "/cleanRanking", method = RequestMethod.POST)
    public ApiResultDTO cleanRanking() {
        rankingDomainService.cleanRanking();
        return ApiResultDTO.success();
    }
}


